/** @file
 * Filesystem utility definitions
 *
 * (c) 2024, Lau
 *
 */

#ifndef _PI_FILESYSTEM_H
#define _PI_FILESYSTEM_H

#include <stdbool.h>

#ifdef __cplusplus
extern "C" {
#endif /* __cplusplus */

/*
 * Default profile name.
 */
#define DEFAULT_PROFILE      "Default"

#define DIR_SEPARATOR "/"

/*
 * Get the directory in which the main (Wireshark, TShark, Logray, etc)
 * program resides.
 * Extcaps should use get_extcap_dir() to get their path.
 *
 * @return The main program file directory.
 */
const char *get_progfile_dir(void);

/*
 * Get the directory in which the plugins are stored.
 */
const char * get_plugins_dir(void);

const char * get_systemfile_dir(void);

/*
 * Construct the path name of a non-extcap Wireshark executable file,
 * given the program name.  The executable name doesn't include ".exe";
 * append it on Windows, so that callers don't have to worry about that.
 *
 * This presumes that all non-extcap executables are in the same directory.
 *
 * The returned file name was g_malloc()'d so it must be g_free()d when the
 * caller is done with it.
 */
char *get_executable_path(const char *filename);

/*
 * Return an error message for UNIX-style errno indications on open or
 * create operations.
 */
const char *file_open_error_message(int err, bool for_writing);

/*
 * Return an error message for UNIX-style errno indications on write
 * operations.
 */
const char *file_write_error_message(int err);

/*
 * Given a pathname, return the last component.
 */
const char *get_basename(const char *);

 /*
  * Given a pathname, return a pointer to the last pathname separator
  * character in the pathname, or NULL if the pathname contains no
  * separators.
  */
char *find_last_pathname_separator(const char *path);

/*
 * Given a pathname, return a string containing everything but the
 * last component.  NOTE: this overwrites the pathname handed into
 * it....
 */
char *get_dirname(char *);

/*
 * Given a pathname, return:
 *
 *    the errno, if an attempt to "stat()" the file fails;
 *
 *    EISDIR, if the attempt succeeded and the file turned out
 *    to be a directory;
 *
 *    0, if the attempt succeeded and the file turned out not
 *    to be a directory.
 */
int is_directory(const char *);

/*
 * Given a pathname, return:
 *
 *    the errno, if an attempt to "stat()" the file fails;
 *
 *    ESPIPE, if the attempt succeeded and the file turned out
 *    to be a FIFO;
 *
 *    0, if the attempt succeeded and the file turned out not
 *    to be a FIFO.
 */
int is_fifo(const char *);

/*
 * Given a pathname, return true if the attempt to "stat()" the file
 * succeeds, and it turns out to be a regular file. "stat()" follows
 * links, so returns true if the pathname is a link to a regular file.
 */
bool is_regular_file(const char *);

/*
 * Check if a file exists.
 */
bool file_exists(const char *fname);

/*
 * Check if file is existing and has text entries which does not start
 * with the comment character.
 */
bool config_file_exists_with_entries(const char *fname, char comment_char);

#ifdef __cplusplus
}
#endif /* __cplusplus */

#endif /* _PI_FILESYSTEM_H */
